//获取要修改的信息
var userData;
//权限控制页面显示框
if(loginUserType == 1){
    $("#systemDictionShow").show();
}else {
    $("#systemDictionShow").hide();
}

var UpdateUserNameFunction = function(){
    $.ajax({
        url:"usersWeb/findUserByUserName.json",
        type:'POST',
        dataType:"json",
        data:{
            userName:addOrUpdateUserName
        },
        success:function(result){
            userData = result.user;
            $("input[name='id']").val(result.user.id);
            $("input[name='username']").val(result.user.username);
            $("#usernameshow").html(result.user.username);
            $("input[name='cdName']").val(result.user.cnName);
            $("input[name='unionName']").val(result.user.trueName);
            $("input[name='idCard']").val(result.user.identity);
            $("input[name='email']").val(result.user.email);
            $("[name='descn']").text(result.user.descn);
            $("[name='tagNames']").text(result.user.tagNames);
            $("input[name='address']").val(result.user.address);
            $("#preview").attr("src","http://img.boyitongcheng.com/" + result.user.bizCover);

            //搜索关键字查询
            var systemAddUserKeywordFunction = function(){
                $.ajax({
                    url:"usersWeb/findAllByChannelKeywordByUserChannel.json",
                    type:'POST',
                    dataType:"json",
                    data:{
                        userName:addOrUpdateUserName
                    },
                    success:function(result){
                        var tem = "";
                        $.each( result.byChannelKeywordList, function(index, content){
                            if(content.type == null || content.type == 0){
                                tem += "<label><input type='checkbox' name='userBox' class='selectDiv' value='"+content.id+"'/>"+content.name+"</label>";
                            }else {
                                tem += "<label class='on'><input type='checkbox' name='userBox' class='selectDiv' checked='checked' value='"+content.id+"'/>"+content.name+"</label>";
                            }
                        });
                        $("#systemAddUserKeyword").empty();
                        $("#systemAddUserKeyword").append(tem);

                        //动态加class
                        $(".userDiv ul li p label").click(function(){
                            $(this).toggleClass("on");
                        });
                    }
                });
            }();

            //权限角色
            var dictionFunction = function(){
                $.ajax({
                    url:"systemRole/findAllRoleByTypePage.json",
                    type:'GET',
                    dataType:"json",
                    data:{
                        roleType:1,page:1,pageNum:1000
                    },
                    success:function(result){
                        var tem = "<option value=''>请选择权限角色</option>";
                        $.each( result.roleList, function(index, content){
                            if(loginUserType == '1'){
                                if(userData.diction == content.roleName){
                                    tem += "<option value='"+content.id+"' selected='selected'>"+content.roleName+"</option>";
                                }else {
                                    tem += "<option value='"+content.id+"'>"+content.roleName+"</option>";
                                }
                            }else {
                                if(content.code != '001'){
                                    if(userData.diction == content.roleName){
                                        tem += "<option value='"+content.id+"' selected='selected'>"+content.roleName+"</option>";
                                    }else {
                                        tem += "<option value='"+content.id+"'>"+content.roleName+"</option>";
                                    }
                                }
                            }
                        });
                        $("#diction").empty();
                        $("#diction").append(tem);
                    }
                });
            }();

            //职业角色
            var occupationFunction = function(){
                $.ajax({
                    url:"systemRole/findAllRoleByTypePage.json",
                    type:'GET',
                    dataType:"json",
                    data:{
                        roleType:2,page:1,pageNum:1000
                    },
                    success:function(result){
                        var tem = "<option value=''>请选择职业角色</option>";
                        $.each( result.roleList, function(index, content){
                            if(userData.occupation == content.roleName){
                                tem += "<option value='"+content.id+"' selected='selected'>"+content.roleName+"</option>";
                            }else {
                                tem += "<option value='"+content.id+"'>"+content.roleName+"</option>";
                            }
                        });
                        $("#occupation").empty();
                        $("#occupation").append(tem);

                    }
                });
            }();

            //佣金角色
            var commissionFunction = function(){
                $.ajax({
                    url:"systemRole/findAllRoleByTypePage.json",
                    type:'GET',
                    dataType:"json",
                    data:{
                        roleType:3,page:1,pageNum:1000
                    },
                    success:function(result){
                        var tem = "<option value=''>请选择佣金角色</option>";
                        $.each( result.roleList, function(index, content){
                            if(userData.commission == content.roleName){
                                tem += "<option value='"+content.id+"' selected='selected'>"+content.roleName+"</option>";
                            }else {
                                tem += "<option value='"+content.id+"'>"+content.roleName+"</option>";
                            }
                        });
                        $("#commission").empty();
                        $("#commission").append(tem);

                    }
                });
            }();

            //信誉角色
            var credibilityFunction = function(){
                $.ajax({
                    url:"systemRole/findAllRoleByTypePage.json",
                    type:'GET',
                    dataType:"json",
                    data:{
                        roleType:4,page:1,pageNum:1000
                    },
                    success:function(result){
                        var tem = "<option value=''>请选择信誉角色</option>";
                        $.each( result.roleList, function(index, content){
                            if(userData.credibility == content.roleName){
                                tem += "<option value='"+content.id+"' selected='selected'>"+content.roleName+"</option>";
                            }else {
                                tem += "<option value='"+content.id+"'>"+content.roleName+"</option>";
                            }
                        });
                        $("#credibility").empty();
                        $("#credibility").append(tem);

                    }
                });
            }();

            var countyFunctionM = function(){
                $("#county").empty();
                $("#county").append("<option value='"+userData.area+"' selected='selected'>"+userData.area+"</option>");
                $(".loginBox .loginDiv ul li .loginSelect1:nth-child(3)").css({'margin-right':'0','float':'right','position':'absolute','right':'0','top':'0'});
            }();

            var cityFunctionM = function(){
                $("#city").empty();
                $("#city").append("<option value='"+userData.city+"' selected='selected'>"+userData.city+"</option>");
                $(".loginBox .loginDiv ul li .loginSelect1:nth-child(3)").css({'margin-right':'0','float':'right','position':'absolute','right':'0','top':'0'});
            }();

            //获取开通省份
            var provinceFunction = function(){
                $.ajax({
                    url:"cityWeb/findAllProvince.json",
                    type:'GET',
                    dataType:"json",
                    success:function(result){
                        var tem = "<option value=''>省份</option>";
                        $.each( result.byOpenCityList, function(index, content){
                            if(userData.province == content.province){
                                tem += "<option value='"+content.province+"' selected='selected'>"+content.province+"</option>";
                            }else {
                                tem += "<option value='"+content.province+"'>"+content.province+"</option>";
                            }
                        });
                        $("#province").empty();
                        $("#province").append(tem);
                        $(".loginBox .loginDiv ul li .loginSelect1:nth-child(3)").css({'margin-right':'0','float':'right','position':'absolute','right':'0','top':'0'});
                    }
                });
            }();

        }
    });
}();

//根据市查询所有区
var countyEditFunction = function(city){
    $.ajax({
        url:"cityWeb/findAllAreaByCity.json",
        type:'POST',
        dataType:"json",
        data:{
            city:city
        },
        success:function(result){
            var tem = "<option value=''>区域</option>";
            $.each( result.byOpenCityList, function(index, content){
                if(userData.area == content.area){
                    tem += "<option value='"+content.area+"' selected='selected'>"+content.area+"</option>";
                }else {
                    tem += "<option value='"+content.area+"'>"+content.area+"</option>";
                }
            });
            $("#county").empty();
            $("#county").append(tem);
            $(".loginBox .loginDiv ul li .loginSelect1:nth-child(3)").css({'margin-right':'0','float':'right','position':'absolute','right':'0','top':'0'});
        }
    });
}

//根据省份查询所有市
var cityEditFunction = function(province){
    $.ajax({
        url:"cityWeb/findAllCityByProvince.json",
        type:'POST',
        dataType:"json",
        data:{
            province:province
        },
        success:function(result){
            var tem = "<option value=''>城市</option>";
            $.each( result.byOpenCityList, function(index, content){
                if(userData.city == content.city){
                    tem += "<option value='"+content.city+"' selected='selected'>"+content.city+"</option>";
                }else {
                    tem += "<option value='"+content.city+"'>"+content.city+"</option>";
                }
            });
            $("#city").empty();
            $("#city").append(tem);
            $(".loginBox .loginDiv ul li .loginSelect1:nth-child(3)").css({'margin-right':'0','float':'right','position':'absolute','right':'0','top':'0'});
        }
    });
}

function provinceEditChange(){
    var options=$("#province option:selected");
    cityEditFunction(options.val());
    countyEditFunction(options.val());
}

function cityEditChange () {
    var options=$("#city option:selected");
    countyEditFunction(options.val());
}

function areaEditChange(){
    var options=$("#county option:selected");
    $.ajax({
        url:"usersWeb/findAllUserByAreaType.json",
        type:'POST',
        dataType:"json",
        data:{
            area:options.val()
        },
        success:function(result){
            var tem = "<option value=''>所属代理商</option>";
            $.each( result.agents, function(index, content){
                tem += "<option value='"+content.id+"'>"+content.username+"</option>";
            });
            $("#editUserAgent").empty();
            $("#editUserAgent").append(tem);
            $(".loginBox .loginDiv ul li .loginSelect1:nth-child(3)").css({'margin-right':'0','float':'right','position':'absolute','right':'0','top':'0'});
        }
    });
}

var areaChangeFrist = function(){
    $.ajax({
        url:"usersWeb/findAllUserByAreaType.json",
        type:'POST',
        dataType:"json",
        data:{
            area:loginArea
        },
        success:function(result){
            var tem = "<option value=''>所属代理商</option>";
            $.each( result.agents, function(index, content){
                tem += "<option value='"+content.id+"'>"+content.username+"</option>";
            });
            $("#editUserAgent").empty();
            $("#editUserAgent").append(tem);
            $(".loginBox .loginDiv ul li .loginSelect1:nth-child(3)").css({'margin-right':'0','float':'right','position':'absolute','right':'0','top':'0'});
        }
    });
}();

//表单提交
var saveFunction = function(){
    if($('input[name="userBox"]:checked').size() > 0){
        var userBox = $('input[name="userBox"]:checked');
        var userChannels = '';
        $.each(userBox, function(index, content){
            userChannels += content.defaultValue + ',';
        });
        userChannels = userChannels.substr(0,userChannels.length-1);
        $("input[name='userChannel']").val(userChannels);
    }
    $("#updateUserForm").ajaxForm({
        dataType: 'json',
        success: function (data) {
            if(data.r == 'OK'){
                hintsPValueFucntion("操作成功");
                $('#updateUserForm')[0].reset();
                navPills('systemUserControlHtml',2);
            }else {
                hintsPValueFucntion("操作失败");
            }
        },
        error:function () {
            hintsPValueFucntion("请求失败");
        }
    });
    // $.ajax({
    //     url:"registerWeb/systemUserInsertOrUpdate.json",
    //     type:'POST',
    //     dataType:"json",
    //     data:$("#updateUserForm").serialize(),
    //     success:function(result){
    //         if(result.isOK == 'OK'){
    //             hintsPValueFucntion("修改成功");
    //             $('#updateUserForm')[0].reset();
    //         }else {
    //             hintsPValueFucntion(result.msg);
    //         }
    //     },
    //     error:function(){
    //         hintsPValueFucntion("请求失败");
    //     }
    // });
}

//重置
var cancelFunction = function(){
    navPills('systemUserControlHtml',2);
}

var closeDivFunction = function (){
    $(".deskDiv").fadeOut();
    $(".roleDele").fadeOut();
    $(".roleNew").fadeOut();
    $(".hints").fadeOut();
    $(".authority").fadeOut();
    $(".modifyOrder").fadeOut();
    $(".hintsDiv").fadeOut();
    $(".teamDiv").fadeOut();
    $(".refundDiv").fadeOut();
    $(".addNote").fadeOut();
    $(".evaluation").fadeOut();
}

//关闭
$(".cancel,.confirm").click(function(){
    closeDivFunction();
});

//友情提示
var hintsPValueFucntion = function(v){
    $("#hintsPValue").html(v);
    $(".hints").css({
        top:($(window).height()-$(".hints").height())/2+$(window).scrollTop()+"px",
        display:"block"
    });
}